<template>
  <div>
    <!-- 弹框 -->
    <!-- .sync 不需要手动绑定一个close自定义事件 -->
    <el-dialog
      title="提示"
      :visible="showDialog"
      width="30%"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- 表单 -->
      <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <!-- value 选中的值   label 中文文案 -->
            <el-option
              v-for="item in employeeList"
              :key="item.id"
              :value="item.username"
              :label="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
        </el-form-item>
      </el-form>
      <!-- 具名插槽 -->
      <template #footer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employee'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean
    },
    curId: {
      type: String,
      default: ''
    },
    codeList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const validateCode = (rule, value, callback) => {
      let _codeList = []
      if (this.form.id) {
        _codeList = this.codeList.filter(item => item !== this.cacheCode)
      } else {
        _codeList = this.codeList
      }
      if (_codeList.includes(value)) {
        callback(new Error('code码不能出现重复'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, message: '部门编码不能重复', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      employeeList: [],
      cacheCode: null
    }
  },
  mounted() {
    this.hGetEmployeeList()
  },
  methods: {
    // 点击 x 关闭弹层
    closeDialog() {
      this.$emit('close-dialog')
      // 初始化数据 清除校验结果
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.addForm.resetFields()
    },
    // 获取员工简单列表数据
    async hGetEmployeeList() {
      const res = await getEmployeeSimple()
      // console.log(res.data)
      this.employeeList = res.data
    },
    openDialog() {
      this.hGetEmployeeList()
      // this.getDetail()
    },
    confirm() {
      // 兜底校验
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.form.id) {
            await updateDepartments(this.form)
          } else {
            // 提交 调用新增接口
            await addDepartments({ ...this.form, pid: this.curId })
          }
          // 关闭弹框
          this.closeDialog()
          // 通知父组件更新列表
          this.$emit('update-list')
        }
      })
    },
    async getDetail(id) {
      const res = await getDepartDetail(id)
      // console.log(res)
      this.form = res.data
      this.cacheCode = res.data.code
    }
  }
}
</script>

<style>

</style>
